#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# GIFBLUR.PY                   -- by Dr. ZhuoQing 2021-02-21
#
# gifblur radius        # Blur select pic by gaussian filter
#                       # with radius
# gifblur ratio         # Blur select gif side
# gifblur 1             # Add the side
#
# Note:
#============================================================

from head import *
from PIL import Image,ImageFilter,ImageDraw

#------------------------------------------------------------
def picside(picfile):
    image = Image.open(picfile)
    imgdata = image.getdata()
    imgwidth = imgdata.size[0]-1
    imgheight = imgdata.size[1]-1


    draw = ImageDraw.Draw(image)
    draw.line((0, 0, imgwidth, 0), fill=(0xc0, 0xc0, 0xc0), width=1)
    draw.line((0, 0, 0, imgheight), fill=(0xc0, 0xc0, 0xc0), width=1)
    draw.line((imgwidth, imgheight, imgwidth, 0), fill=(0xc0, 0xc0, 0xc0), width=1)
    draw.line((imgwidth, imgheight, 0, imgheight), fill=(0xc0, 0xc0, 0xc0), width=1)


    image.save(picfile)


#------------------------------------------------------------
def picblur(picfile, radius=20):
    if radius == 1:
        picside(picfile)
        return


    image = Image.open(picfile)
    imagenew = image.filter(ImageFilter.GaussianBlur(radius=radius))
    imagenew.save(picfile)


#------------------------------------------------------------
if len(sys.argv) == 1:
    printf("gifblur 1 :   Add the gif side")
    printf("gifblur >=2 : Filter image")
    printf("gifblur < 1.0 : Add the gif blur side\a")
    exit()


#------------------------------------------------------------
gifid = tspgetgifid()

if len(gifid) > 0:
    if len(sys.argv) > 1:
        ratio = float(sys.argv[1])

        if ratio > 1.0:
            gifid = ()

#------------------------------------------------------------
if len(gifid) == 0:
    selid = tspgetselectid()

    if len(selid) > 0:
        dopfile = tspgetdopfile(selid[0])
        printf(dopfile)

        radius = 10

        if len(sys.argv) > 1:
            radius = int(sys.argv[1])

        picblur(dopfile)

        printf("\a")
        tsprefreshimagebuffer()
        tsprv()
    else:
        filename = tspgetactionfile()
        if os.path.isfile(filename):
            printF(filename)


        printf("No picture select !\a")

    exit()


#------------------------------------------------------------
viewsize = tspgetviewdcrange()
viewWidth = viewsize[4]
viewHeight = viewsize[5]

#------------------------------------------------------------
dopfile = tspgetdopfile(gifid[0])
gifpages = tspgetgifpage(gifid[0])

filepath = os.path.dirname(dopfile)
filebase = os.path.basename(dopfile)
fileext = filebase.split('.')[1]

#------------------------------------------------------------
gifviewratio = 0.8

def pic2blurside(picfile, ratio):
    if ratio == 1:
        picside(picfile)
        return

    #--------------------------------------------------------
    image = Image.open(picfile).convert('RGB')
    imgdata = image.getdata()

    imgwidth = imgdata.size[0]
    imgheight = imgdata.size[1]

    #--------------------------------------------------------
    if imgwidth/imgheight > viewWidth/viewHeight:
        view0_w = imgwidth
        view0_h = imgwidth * viewHeight / viewWidth
        viewi_h = imgheight
        viewi_w = imgheight * viewWidth / viewHeight
    else:
        view0_h = imgheight
        view0_w = imgheight * viewWidth / viewHeight
        viewi_w = imgwidth
        viewi_h = imgwidth * viewHeight / viewWidth

    view1_w = int(view0_w / ratio)
    view1_h = int(view0_h / ratio)

    #--------------------------------------------------------
    left = int((imgwidth - viewi_w) / 2)
    right = imgwidth - left
    top = int((imgheight - viewi_h) / 2)
    bottom = imgheight - top

    imgcrop = image.crop((left, top, right, bottom))
    imgview = imgcrop.resize((view1_w, view1_h), resample=1)
    imgview = imgview.filter(ImageFilter.GaussianBlur(radius=20))

    left = int((view1_w - imgwidth)/2)
    top = int((view1_h - imgheight)/2)
    imgview.paste(image, (left, top))


    imgview.save(picfile)




#------------------------------------------------------------

if len(sys.argv) > 1:
    gifviewratio = float(sys.argv[1])

printf("Proc GIF:%s/%d/%5.2f\a"%(filebase, gifpages, gifviewratio))

#------------------------------------------------------------
for i in range(gifpages):
    filename1 = '%04d.%s'%(i, fileext)
    giffile = os.path.join(filepath, filename1)

    tspshowinfor('%d '%i)

    pic2blurside(giffile, gifviewratio)



#------------------------------------------------------------
printf("\a")
tsprefreshimagebuffer()
tsprv()





#------------------------------------------------------------
#        END OF FILE : GIFBLUR.PY
#============================================================
